<?php
/**
 * Created by PhpStorm.
 * Redis连接实例
 * User: gxk
 * Date: 2019/4/19
 * Time: 17:54
 */

namespace Library;


class Redis
{
    private static $redis;
    private function __construct()
    {

    }
    /*
     * 获取实例
     */
    public static function connect()
    {
        if(empty(self::$redis)){
            self::redisConnect();
        }
        return self::$redis;
    }
    /*
     * 连接redis
     */
    private function redisConnect()
    {
        self::$redis = new \redis();
        $host = C('settings','redis','host');
        $port = C('settings','redis','port');
        $connect = C('settings','redis','pconnect')?'pconnect':'connect';
        $timeout = C('settings','redis','timeout');
        $auth = C('settings','redis','auth');
        $dbname = C('settings','redis','dbname');
        $prefix = C('settings','redis','prefix');
        if(empty($timeout)){
                self::$redis->$connect($host,$port);
        }else{
                self::$redis->$connect($host,$port,$timeout);
        }
        if(!empty($auth)){
            self::$redis->auth($auth);
        }
//        self::$redis->select($dbname);
        if(!empty($prefix)){
            self::$redis->setOption(\Redis::OPT_PREFIX,$prefix);
        }
    }
    private function __clone()
    {
        // TODO: Implement __clone() method.
    }
}